#include "IocpThread.h"

IocpThread::IocpThread()
	: Iocp(), Thread(), m_bReturn(false), m_ulTransferred(0), m_pulCompletionkey(NULL)
	, m_pkOverlapped(NULL), m_ulWaitTime(INFINITE)
{
}

IocpThread::~IocpThread()
{
}

void IocpThread::Post(void *pvArg)
{
	PostQueuedCompletionStatus(0, (ulong *)pvArg, NULL);
}

void IocpThread::SetIocpWaitTime(ulong time)
{
	m_ulWaitTime = time;
}

void IocpThread::Stop()
{
	m_bRun = false;
	PostQueuedCompletionStatus(0, NULL, NULL);
}


void IocpThread::Worker()
{
	m_bReturn = GetQueuedCompletionStatus(&m_ulTransferred, (PULONG_PTR)&m_pulCompletionkey, &m_pkOverlapped, m_ulWaitTime);
	if( m_bReturn )
	{
		IocpWork((void *)m_pulCompletionkey);
	}
	else
	{
		Error((void *)m_pulCompletionkey, GetLastError());
	}
}